/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.object;

import java.util.logging.Level;
import java.util.logging.Logger;
import net.worlds.WorldsException;
import net.worlds.Main;
import net.worlds.PropChangeHandler;
import net.worlds.net.Server;
import se.krka.kahlua.vm.JavaFunction;
import se.krka.kahlua.vm.LuaCallFrame;


public class NetworkServer extends GameObject{
    private int port;
    private Server server;
    public NetworkServer() throws WorldsException
    {
        super("NetworkServer");
        addPropChangeHandler("Port", new PropChangeHandler()
        {
            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                port = (Integer) value;
            }
        });
        addFunction("start", new JavaFunction()
        {
            public int call(LuaCallFrame callFrame, int nArguments) {
                start();
                return 0;
            }
        });
        //x_;
    }
    public void start()
    {
        try {
            server = new Server(port, getIntProp("MaximumPlayers"), getIntProp("GameID"));
        } catch (WorldsException ex) {
            ex.printStackTrace();
        }
        Main.setServer(server);
    }
    @Override
    public void destructor() throws WorldsException
    {
        super.destructor();
        System.err.println("Stopping server");
        if(server != null)
            server.stop();
    }
}
